﻿<?php
	$imgName=$_FILES['image']['name'];	
	if($imgName!="")
	{
		$imgType=$_FILES['image']['type'];
		if(strpos($imgType,"image/")===false)
		{
			echo "<script>alert('Vui lòng chọn file có định dạng hình ảnh!');history.go(-1)</script>";
			return;
		}
	
		$imgSize=$_FILES['image']['size'];	
		if($imgSize>1024*1024)
		{
			echo "<script>alert('Vui lòng chọn file có kích thước nhỏ hơn 1MB!');history.go(-1)</script>";
			return;
		}
		
		
		$imgName=str_replace(" ","",$imgName);
		$imgName=str_replace("-","",$imgName);
		$imgName=date("y_m_d_h_m_s").(strlen($imgName)>4?substr($imgName,strlen($imgName)-4):"");
		if(!move_uploaded_file($_FILES['image']['tmp_name'],"images/product/".$imgName))
		{
			echo "<script>alert('Lỗi trong quá trình upload file. Vui lòng thử lại!');history.go(-1)</script>";
			return;
		}
		chmod("images/product/".$imgName,0644);
		thumbnail($imgName,$imgType,136,125,"images/product/","images/product/thumbnail/");
	}
					
	$sql="INSERT INTO tbl_product	(code			,`name`			,image				,alt			,quote				,detail					,price				,`unit`			,`product_type_id`)
							VALUES	('$_POST[code]'	,'$_POST[name]'	,'$imgName'	,		'$_POST[alt]'	,'$_POST[quote]'	,'$_POST[detail]'		,'$_POST[price]'	,'$_POST[unit]'	,'$_POST[product_type_id]')";
	
	$res=mysql_query($sql);
	if(!$res)
	{
		@unlink("images/product/$imgName");
		@unlink("images/product/thumbnail/$imgName");
		echo "<script>history.go(-1)</script>";
	}
	else 
	{
		echo "<script>window.location='?zone=product'</script>";
	}
?>
